package com.example.wificardemo.slice;

import com.example.wificardemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.TextField;
import ohos.global.resource.Resource;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel hilog = new HiLogLabel(HiLog.DEBUG ,0x0000, "test0002");
    SocketTest ST = null;

    ProgressBar[] PB = new ProgressBar[11];
    @Override
    public void onStart(Intent intent) {
        int pbs[] = {ResourceTable.Id_progressbar,ResourceTable.Id_progressbar1,ResourceTable.Id_progressbar2,ResourceTable.Id_progressbar3,ResourceTable.Id_progressbar4,ResourceTable.Id_progressbar5,ResourceTable.Id_progressbar6,ResourceTable.Id_progressbar7,ResourceTable.Id_progressbar8,ResourceTable.Id_progressbar9,ResourceTable.Id_progressbar10};
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button connects = findComponentById(ResourceTable.Id_connect_car);
        Button bt1 = findComponentById(ResourceTable.Id_button1);
        Button bt2 = findComponentById(ResourceTable.Id_button2);
        Button bt3 = findComponentById(ResourceTable.Id_button3);
        Button bt4 = findComponentById(ResourceTable.Id_button4);
        Button bt5 = findComponentById(ResourceTable.Id_button5);

        TextField text_field = findComponentById(ResourceTable.Id_text_field);

        for(int i = 0;i<11;i++)
        {
            PB[i] = (ProgressBar)findComponentById(pbs[i]);
        }
        bt1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ST.sockerSend(1,1);
            }
        });

        bt2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ST.sockerSend(1,4);
            }
        });
        bt3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ST.sockerSend(1,0);
            }
        });

        bt4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ST.sockerSend(1,2);
            }
        });

        bt5.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ST.sockerSend(1,3);
            }
        });

        connects.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(ST !=null)
                {
                    ST = null; //释放
                }
                try {
                    ST = new SocketTest(MainAbilitySlice.this,text_field.getText().toString(),20222);
                    ST.start();
                    Thread.sleep(200);
                    //ST.sockerSend(5,0,0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //Socker 接收回调函数
    public void socketCallback(Elements con) {
        //HiLog.debug(hilog,""+(int)data[0]+" "+data[1]+" "+data[2]+" "+data[3],"");

        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                PB[con.angle].setProgressValue(con.cm>100 ? 100:(int)con.cm);
                //tx.setText(inSetValue+"");
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
